//index.js
//获取应用实例
const userService = require('../../services/userService');
var app = getApp()
Page({
  data: {
    userInfo: {},
    red_info: [],
    rebate_info: [],
    store_info: '',
    qrcode: ''
  },
  navigatorHB: function() {
    wx.navigateTo({
      url: '../hongbao/hongbao'
    })
  },
  navigatorEncourage: function() {
    wx.navigateTo({
      url: '../encourage/encourage'
    })
  },
  navigatorDrawcash: function() {
    var that = this;
    if(that.data.store_info.balance == 0) {
      wx.showToast({
        title: '您没有可以提现的金额！'
      });
      return;
    }
    wx.navigateTo({
      url: '../drawcash/drawcash?balance=' + that.data.store_info.balance
    })
  },
  bindScanCode: function() {
    var that = this;
    app.bindScanCode(function(qrcode){
      that.setData({
        qrcode: qrcode
      });
    });
  },
  getIndexInfo: function() {
    var that = this;
    var user = wx.getStorageSync('user');
    if(user) {
      if(!user.openid) {
        wx.showToast({
          title:'未获取到用户openid'
        });
        return;
      }
      wx.showLoading({
        title: '加载数据...'
      });
      userService.getIndexInfo(user.openid).then((data) => {
        var data = data.data;
        that.setData({
          red_info: data.red_info,
          rebate_info: data.rebate_info,
          store_info: data.store_info
        });
        wx.hideLoading();
      }, (error) => {
      });
    }
  },
  onLoad: function (options) {
    //TODO 测试用
    // wx.navigateTo({
    //   url: '../activity/activity?qrcode=o5510aLo'
    // });
    if(options.q) {
      this.setData({
        qrcode: decodeURIComponent(options.q)
      });
      var qurl = decodeURIComponent(options.q);
      var qrcode = qurl.substring(qurl.lastIndexOf('/') + 1);
      wx.redirectTo({
        url: '../activity/activity?qrcode=' + qrcode
      });
    } else {
      var that = this;
      this.getIndexInfo();
      app.editTabBar();//添加tabBar数据  
      
      //调用应用实例的方法获取全局数据
      app.getUserInfo(function(userInfo){
        //更新数据
        that.getIndexInfo();
        that.setData({
          userInfo:userInfo
        });
      });
    }
   
    
  }
})
